package demo1.NiceDemo.mouse;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class MyMouse extends JPanel {
    private boolean highLight;
    private boolean pressed;
    private final List<Lines> linesList = new ArrayList<>();
    private Color penColor = new Color(0,0,0);
    private int changeNums = 0;
    private boolean flag;

    public MyMouse() {
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                highLight = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
                highLight = false;
                repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                pressed = true;
                if(flag) {
                    Lines lines = linesList.get(linesList.size() - 1);
                    lines.points.add(e.getPoint());
                    flag = false;
                }else {
                    Lines lines = new Lines();
                    lines.points.add(e.getPoint());
                    linesList.add(lines);
                }
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                pressed = false;
            }
        });
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                if (pressed) {
                    Lines lines = linesList.get(linesList.size() - 1);
                    lines.points.add(e.getPoint());
                    repaint();
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = this.getWidth();
        int height = this.getHeight();
        g.setColor(new Color(255, 255, 255, 255));
        g.fillRect(0, 0, width, height);
        //鼠标移入，高亮边框
        if (highLight) {
            g.setColor(new Color(84, 84, 84));
            g.drawRect(0, 0, width - 1, height - 1);
        } else {
            g.setColor(new Color(183, 183, 183));
            g.drawRect(0, 0, width - 1, height - 1);
        }
        g.setColor(new Color(0,0,0));
        //绘制涂鸦
        for (Lines lines : linesList) {
            if (lines.points.size() >= 2) {
                g.setColor(lines.penColor);
                Point p1 = lines.points.get(0);
                for (int i = 0; i < lines.points.size(); i++) {
                    Point p2 = lines.points.get(i);
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                    p1 = p2;
                }
            }
        }
    }

    public void changeColor() {
        switch (changeNums) {
            case 0:
                penColor = new Color(255, 0, 0);
                break;
            case 1:
                penColor = new Color(255, 228, 13);
                break;
            case 2:
                penColor = new Color(0, 128, 255);
                break;
            case 3:
                penColor = new Color(79, 224, 100);
                break;
            case 4:
                penColor = new Color(3, 3, 3);
                break;
        }
        changeNums = (changeNums + 1) % 5;
    }

    public Lines CtrlZ() {
        if (linesList.size() == 0)
            return null;
        Lines ret = linesList.get(linesList.size() - 1);
        linesList.remove(linesList.size() - 1);
        repaint();
        return ret;
    }

    public void CtrlY(Lines lines) {
        if (lines == null)
            return;
        linesList.add(lines);
        repaint();
    }

    public void clearScreen() {
        linesList.clear();
        changeNums = 0;
        penColor = new Color(0,0,0);
        repaint();
    }

    public void changeFlag() {
        flag = true;
        Lines lines = new Lines();
        changeColor();
        lines.penColor = penColor;
        linesList.add(lines);
    }

    public Color getPenColor() {
        return penColor;
    }

    public static class Lines {
        public List<Point> points = new ArrayList<>();
        public Color penColor;
    }
}
